package com.vnp.vchat.version2;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.vnp.vchat.R;
import com.vnp.vchat.httpclient.HttpClientHelper;
import com.vnp.vchat.httpclient.MyJsonHttpResponseHandler;
import com.vnp.vchat.util.Common;
import com.vnp.vchat.util.PreferenceHelper;
import com.vnp.vchat.util.Utils;

public class ThongbaoActivity extends Activity implements OnClickListener, OnTouchListener{

	ImageButton btnBack;
	TextView title, titleTinnhanmoi, titleHoatdongKH;
	
	ImageButton btnOnOffTinnhan, btnOnOffHoatdong;
	
	// settings
	int notification_tinnhan;
	int notification_hoatdong;
	int notification_hienthi;
	PreferenceHelper preferenceHelper;
	
	int state;
	
	Runnable rS = new Runnable() {
		
		@Override
		public void run() {
			if (state == 1) {
				if (notification_tinnhan == 1){
					notification_tinnhan = 0;
					btnOnOffTinnhan.setImageResource(R.drawable.btn_off);
				} else if (notification_tinnhan == 0) {
					notification_tinnhan = 1;	
					btnOnOffTinnhan.setImageResource(R.drawable.btn_on);
				}
				
				preferenceHelper.setSettingNotifi(String.valueOf(notification_tinnhan));
				
			} else if (state ==2) {
				if (notification_hoatdong == 1){
					notification_hoatdong = 0;
					btnOnOffHoatdong.setImageResource(R.drawable.btn_off);
				} else if (notification_hoatdong == 0) {
					notification_hoatdong = 1;	
					btnOnOffHoatdong.setImageResource(R.drawable.btn_on);
				}
				
				preferenceHelper.setSettingFeed(String.valueOf(notification_hoatdong));
				
			}
		}
	};
	
	Handler mHandler = new Handler();
	
	@Override
	protected void onResume() {
		super.onResume();
		Common.appRunning = true;
	}
	@Override
	protected void onPause() {
		super.onPause();
		Common.appRunning = false;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_thongbao);
		
		preferenceHelper = new PreferenceHelper(this);
		
		notification_tinnhan = Integer.parseInt(preferenceHelper.getSettingNotifi());
		notification_hoatdong = Integer.parseInt(preferenceHelper.getSettingFeed());
		notification_hienthi = Integer.parseInt(preferenceHelper.getSettingDisplay());
		
		btnBack = (ImageButton) findViewById(R.id.btn_function1);
		btnBack.setVisibility(View.VISIBLE);
		
		title = (TextView) findViewById(R.id.title);
		titleTinnhanmoi = (TextView) findViewById(R.id.title_tinnhanmoi);
		titleHoatdongKH = (TextView) findViewById(R.id.title_hoatdongkhachhang);
		
		btnOnOffTinnhan = (ImageButton) findViewById(R.id.btn_setting_tinnhan);
		btnOnOffHoatdong = (ImageButton) findViewById(R.id.btn_setting_hoatdongkh);
		
		
		init();
		setFont();
		
		showStateButton();
		
	}
	
	void showStateButton(){
		
		
		if (notification_tinnhan == 1){
			btnOnOffTinnhan.setImageResource(R.drawable.btn_on);
		} else {
			btnOnOffTinnhan.setImageResource(R.drawable.btn_off);
		}
		
		if (notification_hoatdong == 1){
			btnOnOffHoatdong.setImageResource(R.drawable.btn_on);
		} else if(notification_hoatdong == 0){
			btnOnOffHoatdong.setImageResource(R.drawable.btn_off);
		}
		
		
	}
	
	void init(){
		title.setText("Cài đặt thông báo");
		btnBack.setOnClickListener(this);
		
		btnOnOffHoatdong.setOnTouchListener(this);
		btnOnOffTinnhan.setOnTouchListener(this);
	}
	
	void setFont(){
		
		title.setTypeface(Utils.getTypefaceBold(ThongbaoActivity.this));
		titleHoatdongKH.setTypeface(Utils.getTypefaceRegular(ThongbaoActivity.this));
		titleTinnhanmoi.setTypeface(Utils.getTypefaceRegular(ThongbaoActivity.this));
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_function1:
			closeActivity();
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		closeActivity();
	}
	
	void closeActivity(){
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.btn_setting_tinnhan:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				state = 1;
				if (notification_tinnhan == 0) {
					notification_tinnhan = 1;
					postSettings(notification_tinnhan, notification_hoatdong, notification_hienthi);
					btnOnOffTinnhan.setImageResource(R.drawable.btn_on);
				} else {
					notification_tinnhan = 0;
					postSettings(notification_tinnhan, notification_hoatdong, notification_hienthi);
					btnOnOffTinnhan.setImageResource(R.drawable.btn_off);
				}
			}
			break;

		case R.id.btn_setting_hoatdongkh:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				state = 2;
				if (notification_hoatdong == 0) {
					notification_hoatdong = 1;
					postSettings(notification_tinnhan, notification_hoatdong, notification_hienthi);
					btnOnOffHoatdong.setImageResource(R.drawable.btn_on);
				} else {
					notification_hoatdong = 0;
					postSettings(notification_tinnhan, notification_hoatdong, notification_hienthi);
					btnOnOffHoatdong.setImageResource(R.drawable.btn_off);
				}
			}
			break;
	}
		return false;
	}
	
	void postSettings(final int st_notifi, final int st_feed, final int st_display){
		HttpClientHelper httpClientHelper = new HttpClientHelper(new MyJsonHttpResponseHandler(){

			@Override
			public void onFailure(Throwable error) {
				super.onFailure(error);
				mHandler.removeCallbacks(rS);
				mHandler.postDelayed(rS, 1000);
			}

			@Override
			public void onSuccess(JSONObject re) {
				
				Log.i("vChat", "Re: "+re.toString());
				try {
					int status = re.getInt("status");
					if (status == 1){
						Log.i("vChat", "Cap nhat thanh cong");
						preferenceHelper.setSettingNotifi(String.valueOf(st_notifi));
						preferenceHelper.setSettingFeed(String.valueOf(st_feed));
						preferenceHelper.setSettingDisplay(String.valueOf(st_display));
					} else {
						mHandler.removeCallbacks(rS);
						mHandler.postDelayed(rS, 1000);
						Log.i("vChat", "Cap nhat that bai");
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
		});
		
		httpClientHelper.postSettings(preferenceHelper.getAdminId(), preferenceHelper.getHash(), st_notifi, st_feed, st_display);
		
	}
}
